package com.zuihai.app.mvp.presenter;

import android.app.Application;

import com.jess.arms.di.scope.FragmentScope;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxUtils;
import com.zuihai.app.mvp.contract.DetailFavourContract;
import com.zuihai.app.mvp.model.entity.BaseJson;
import com.zuihai.app.mvp.model.entity.Favour;
import com.zuihai.app.mvp.model.entity.MessageResponse;
import com.zuihai.app.mvp.ui.adapter.DetailFavourAdapter;
import com.zuihai.app.mvp.ui.utils.AccountHelper;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/6/8 0008.
 */

@FragmentScope
public class DetailFavourPresenter extends BasePresenter<DetailFavourContract.Model, DetailFavourContract.View> {
    private RxErrorHandler mErrorHandler;
    private Application mApplication;
    private AppManager mAppManager;
    private List<Favour.User> mDatas = new ArrayList<>();
    private DetailFavourAdapter mAdapter;
    private int mPageIndex = 1;

    @Inject
    public DetailFavourPresenter(DetailFavourContract.Model model, DetailFavourContract.View rootView
            , RxErrorHandler handler, Application application, AppManager appManager) {
        super(model, rootView);
        this.mErrorHandler = handler;
        this.mApplication = application;
        this.mAppManager = appManager;
        mAdapter = new DetailFavourAdapter(mDatas);
        mRootView.bindAdapter(mAdapter);
    }

    public void secretFavourUsers(String id, boolean isRefresh){
        if (isRefresh) mPageIndex = 1;
        String uid = AccountHelper.getInstance().getUid();
        mModel.secretFavourUsers(id, uid, mPageIndex)
                .retryWhen(new RetryWithDelay(3, 3))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Func1<BaseJson<Favour.User>, List<Favour.User>>() {
                    @Override
                    public List<Favour.User> call(BaseJson<Favour.User> baseJson) {
                        if (baseJson.isSuccess()) {
                            return baseJson.data.result;
                        }
                        return null;
                    }
                })
                .doAfterTerminate(new Action0() {
                    @Override
                    public void call() {
                        if (isRefresh) mRootView.refreshComplete();
                    }
                })
                .compose(RxUtils.<List<Favour.User>>bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<List<Favour.User>>(mErrorHandler) {
                    @Override
                    public void onNext(List<Favour.User> users) {
                        mPageIndex ++;
                        if (isRefresh){
                            mAdapter.setNewData(users);
                        }else{
                            if (users.isEmpty()){
                                mAdapter.loadMoreEnd();
                            }else{
                                mAdapter.addData(users);
                            }
                        }
                    }
                });
    }

    public void headlineFavourUsers(String id, boolean isRefresh){
        if (isRefresh) mPageIndex = 1;
        String uid = AccountHelper.getInstance().getUid();
        mModel.headlineFavourUsers(id, uid, mPageIndex)
                .retryWhen(new RetryWithDelay(3, 3))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Func1<BaseJson<Favour.User>, List<Favour.User>>() {
                    @Override
                    public List<Favour.User> call(BaseJson<Favour.User> baseJson) {
                        if (baseJson.isSuccess()) {
                            return baseJson.data.result;
                        }
                        return null;
                    }
                })
                .doAfterTerminate(new Action0() {
                    @Override
                    public void call() {
                        if (isRefresh) mRootView.refreshComplete();
                    }
                })
                .compose(RxUtils.<List<Favour.User>>bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<List<Favour.User>>(mErrorHandler) {
                    @Override
                    public void onNext(List<Favour.User> users) {
                        mPageIndex ++;
                        if (isRefresh){
                            mAdapter.setNewData(users);
                        }else{
                            if (users.isEmpty()){
                                mAdapter.loadMoreEnd();
                            }else{
                                mAdapter.addData(users);
                            }
                        }
                    }
                });
    }

    public void diaryFavourUsers(String id, boolean isRefresh){
        if (isRefresh) mPageIndex = 1;
        String uid = AccountHelper.getInstance().getUid();
        mModel.diaryFavourUsers(id, uid, mPageIndex)
                .retryWhen(new RetryWithDelay(3, 3))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Func1<BaseJson<Favour.User>, List<Favour.User>>() {
                    @Override
                    public List<Favour.User> call(BaseJson<Favour.User> baseJson) {
                        if (baseJson.isSuccess()) {
                            return baseJson.data.result;
                        }
                        return null;
                    }
                })
                .doAfterTerminate(new Action0() {
                    @Override
                    public void call() {
                        if (isRefresh) mRootView.refreshComplete();
                    }
                })
                .compose(RxUtils.<List<Favour.User>>bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<List<Favour.User>>(mErrorHandler) {
                    @Override
                    public void onNext(List<Favour.User> users) {
                        mPageIndex ++;
                        if (isRefresh){
                            mAdapter.setNewData(users);
                        }else{
                            if (users.isEmpty()){
                                mAdapter.loadMoreEnd();
                            }else{
                                mAdapter.addData(users);
                            }
                        }
                    }
                });
    }

    public void serverFavourUsers(String id, boolean isRefresh){
        if (isRefresh) mPageIndex = 1;
        String uid = AccountHelper.getInstance().getUid();
        mModel.serverFavourUsers(id, uid, mPageIndex)
                .retryWhen(new RetryWithDelay(3, 3))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Func1<BaseJson<Favour.User>, List<Favour.User>>() {
                    @Override
                    public List<Favour.User> call(BaseJson<Favour.User> baseJson) {
                        if (baseJson.isSuccess()) {
                            return baseJson.data.result;
                        }
                        return null;
                    }
                })
                .doAfterTerminate(new Action0() {
                    @Override
                    public void call() {
                        if (isRefresh) mRootView.refreshComplete();
                    }
                })
                .compose(RxUtils.<List<Favour.User>>bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<List<Favour.User>>(mErrorHandler) {
                    @Override
                    public void onNext(List<Favour.User> users) {
                        mPageIndex ++;
                        if (isRefresh){
                            mAdapter.setNewData(users);
                        }else{
                            if (users.isEmpty()){
                                mAdapter.loadMoreEnd();
                            }else{
                                mAdapter.addData(users);
                            }
                        }
                    }
                });
    }

    public void shopFavourUsers(String id, boolean isRefresh){
        if (isRefresh) mPageIndex = 1;
        String uid = AccountHelper.getInstance().getUid();
        mModel.shopFavourUsers(id, uid, mPageIndex)
                .retryWhen(new RetryWithDelay(3, 3))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Func1<BaseJson<Favour.User>, List<Favour.User>>() {
                    @Override
                    public List<Favour.User> call(BaseJson<Favour.User> baseJson) {
                        if (baseJson.isSuccess()) {
                            return baseJson.data.result;
                        }
                        return null;
                    }
                })
                .doAfterTerminate(new Action0() {
                    @Override
                    public void call() {
                        if (isRefresh) mRootView.refreshComplete();
                    }
                })
                .compose(RxUtils.<List<Favour.User>>bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<List<Favour.User>>(mErrorHandler) {
                    @Override
                    public void onNext(List<Favour.User> users) {
                        mPageIndex ++;
                        if (isRefresh){
                            mAdapter.setNewData(users);
                        }else{
                            if (users.isEmpty()){
                                mAdapter.loadMoreEnd();
                            }else{
                                mAdapter.addData(users);
                            }
                        }
                    }
                });
    }

    public void commodtiyFavourUsers(String id, boolean isRefresh){
        if (isRefresh) mPageIndex = 1;
        String uid = AccountHelper.getInstance().getUid();
        mModel.commodtiyFavourUsers(id, uid, mPageIndex)
                .retryWhen(new RetryWithDelay(3, 3))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Func1<BaseJson<Favour.User>, List<Favour.User>>() {
                    @Override
                    public List<Favour.User> call(BaseJson<Favour.User> baseJson) {
                        if (baseJson.isSuccess()) {
                            return baseJson.data.result;
                        }
                        return null;
                    }
                })
                .doAfterTerminate(new Action0() {
                    @Override
                    public void call() {
                        if (isRefresh) mRootView.refreshComplete();
                    }
                })
                .compose(RxUtils.<List<Favour.User>>bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<List<Favour.User>>(mErrorHandler) {
                    @Override
                    public void onNext(List<Favour.User> users) {
                        mPageIndex ++;
                        if (isRefresh){
                            mAdapter.setNewData(users);
                        }else{
                            if (users.isEmpty()){
                                mAdapter.loadMoreEnd();
                            }else{
                                mAdapter.addData(users);
                            }
                        }
                    }
                });
    }

    public void requestCare(String id, int isCare) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.requestCare(id, uid, isCare)
                .retryWhen(new RetryWithDelay(1, 2))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ErrorHandleSubscriber<MessageResponse>(mErrorHandler) {
                    @Override
                    public void onNext(MessageResponse response) {
                        if (response.isSuccess()) {
                            mRootView.showMessage(response.data);
                        } else {
                            mRootView.showMessage(response.data);
                        }
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mApplication = null;
    }
}
